import { Component, OnInit, OnDestroy, OnChanges, AfterContentInit, Input } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { EventManager } from '@angular/platform-browser';
import { AssessmentDetailsModalService } from './assessment-details-modal.service';

interface optionConfig {
  moduleId: string | number, // 机型ID
  evalList: Array<any>, // 对比项
  umname: string, // 机型名称
  totalNum: number // 预估合计
}

@Component({
  selector: 'assessment-details-modal',
  templateUrl: './assessment-details-modal.component.html',
  styleUrls: ['./assessment-details-modal.component.scss']
})
export class AssessmentDetailsModalComponent implements OnInit, OnDestroy {
  // @Input() optionList: any[] = [];
  // 遮罩层 Mask
  @Input() isMask: boolean = true;
  @Input() optionParams: optionConfig = {
    moduleId: '', // 机型ID
    evalList: [], // 对比项
    umname: '', // 机型名称
    totalNum: 0 // 预估合计
  }

  _listData: any = [];
  isVisible: boolean = false;
  constructor(
    private eventManager: EventManager,
    private message: NzMessageService,
    private services: AssessmentDetailsModalService
  ) { }

  ngOnInit() {
    // this.getSubjectList();
    // 键盘 ESC
    this.eventManager.addGlobalEventListener('window', 'keyup.esc', () => {
      if (this.isVisible) {
        this.isVisible = false;
      }
    })
  }

  ngOnDestroy(): void {
    // this.eventManager.getZone
  }

  // 查询评估题目
  async getSubjectList() {
    if (!this.optionParams.moduleId) {
      return
    }
    let params = {
      modelId: this.optionParams.moduleId
    }
    const res: any = await this.services.getContentData(params);

    if (res.code == 0) {
      this._listData = [];
      this.optionParams.evalList.forEach((item: any) => {
        let tmpObj = res.data.find((ele: any) => ele.id == item.qId);
        let obj = {
          id: tmpObj?.id,
          selectType: tmpObj?.selectType,
          questionName: tmpObj?.questionName,
          selectedValue: tmpObj?.selectType == 0 ? Number(item.ids) : item.ids,
          options: tmpObj?.selectType == 0 ? tmpObj?.options : this.checkedFilter(tmpObj?.options, item.ids)
        };
        this._listData.push(obj);
      })
    } else {
      this.message.error(res.message);
    }
  }

  // checkbox options filter
  checkedFilter(options: any, selectedIds: any) {
    let tmpArrs: any = [];
    let selectedArr = selectedIds ? String(selectedIds)?.split(',') : [];

    options.forEach((ele: any) => {
      let newObj = {
        value: ele.id,
        label: ele.optionName,
        checked: selectedArr.includes(String(ele.id)) ? true : false
      };
      tmpArrs.push(newObj);
    });
    return tmpArrs;
  }
}
